package com.wxm.killing.mq.common.util;

import com.wxm.killing.mq.common.exception.BaseException;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.util.StringUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Description TODO
 * @author: wxm
 * @date: 2019/10/27 14:25
 * @version: 1.0
 */
public class DateUtil {
    public final static String HOUR_FORMAT = "HH:mm";

    public static final String YYYYMMddHHMMSS = "yyyyMMddHHmmss";

    /**
     * 计算当前时间是否在区间内
     *
     * @param startTimeStr 开始时间
     * @param endTimeStr 结束时间
     * @return
     */
    public static boolean isEffectiveDate(String startTimeStr, String endTimeStr) {
        try {
            if(StringUtils.isEmpty(startTimeStr)){
                startTimeStr = "00:00";
            }
            if(StringUtils.isEmpty(endTimeStr)){
                endTimeStr = "23:59";
            }
            String []strs1=startTimeStr.split(":");
            String []strs2=endTimeStr.split(":");
            Calendar currentDate = Calendar.getInstance();
            currentDate.setTime(new Date());

            Calendar startTime=Calendar.getInstance();
            startTime.set(Calendar.YEAR, currentDate.get(Calendar.YEAR));
            startTime.set(Calendar.MONTH, currentDate.get(Calendar.MONTH));
            startTime.set(Calendar.DAY_OF_MONTH, currentDate.get(Calendar.DAY_OF_MONTH));
            startTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(strs1[0]));
            startTime.set(Calendar.MINUTE, Integer.parseInt(strs1[1]));
            startTime.set(Calendar.SECOND, 0);

            Calendar endTime=Calendar.getInstance();
            endTime.set(Calendar.YEAR, currentDate.get(Calendar.YEAR));
            endTime.set(Calendar.MONTH, currentDate.get(Calendar.MONTH));
            endTime.set(Calendar.DAY_OF_MONTH, currentDate.get(Calendar.DAY_OF_MONTH));
            endTime.set(Calendar.HOUR_OF_DAY,Integer.parseInt(strs2[0]));
            endTime.set(Calendar.MINUTE, Integer.parseInt(strs2[1]));
            endTime.set(Calendar.SECOND, 0);
            return isEffectiveDate(currentDate,startTime,endTime);
        } catch (Exception e) {
            throw new BaseException(0, "抢购时间配置异常");
        }
    }

    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     * 场景1：开始、结束时间在当天  如果当前时间在开始结束时间之内返回true;
     * 场景2：开始、结束时间跨天 ,如果当前时间在[开始时间-1 ,结束时间] [开始时间 ,结束时间+1]区间内，返回true
     * @param nowTime   当前时间
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @author jqlin
     */
    public static boolean isEffectiveDate(Calendar nowTime, Calendar startTime, Calendar endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }
        //跨天
        if(startTime.getTimeInMillis() > endTime.getTimeInMillis()){
            Calendar startTimeSub = (Calendar) startTime.clone();
            startTimeSub.set(Calendar.DAY_OF_MONTH,(startTimeSub.get(Calendar.DAY_OF_MONTH)-1));
            if(nowTime.getTimeInMillis() >= startTimeSub.getTimeInMillis() && nowTime.getTimeInMillis() <= endTime.getTimeInMillis()){
                return true;
            }
            Calendar endTimeAdd = (Calendar) startTime.clone();
            endTimeAdd.set(Calendar.DAY_OF_MONTH,(endTimeAdd.get(Calendar.DAY_OF_MONTH)+1));
            if(nowTime.getTimeInMillis() >= startTime.getTimeInMillis() && nowTime.getTimeInMillis() <= endTimeAdd.getTimeInMillis()){
                return true;
            }
        }
        //开始结束时间在一天之内
        if(nowTime.getTimeInMillis() >= startTime.getTimeInMillis() && nowTime.getTimeInMillis() <= endTime.getTimeInMillis()){
            return true;
        }
        return false;
    }

    public static String getNowString(String format){
        SimpleDateFormat simpledateformat = new SimpleDateFormat(format);
        return simpledateformat.format(new Date());
    }

    public static void main(String[] args) {
        System.out.println(isEffectiveDate("23:58","09:00"));;
    }
}
